package com.javaspeak.algorithm.bubblesort.recursive;

import java.io.StringWriter;

/**
 * @author John Dickerson
 */
public class RecursiveBubbleSort {

    public static void showArray( String[] names, int counter ){

        StringWriter sw = new StringWriter();

        for ( int i=0; i<names.length; i++ ){

            if ( i == counter -1 ){

                sw.write( "<" + names[ i ] + " " );
            }
            else if ( i == counter ){

                sw.write( names[ i ] + "> " );
            }
            else {

                sw.write( names[ i ] + " " );
            }
        }

        System.out.println( sw.toString() );
    }


    public static void bubbleSort(
            int counter, String[] names, boolean swapped ){

        if ( counter == names.length ){

            if ( swapped ){

                counter = 1;
            }
            else {
                return;
            }
        }

        showArray( names, counter );

        swapped = false;

        if ( names[ counter - 1 ].compareTo( names[ counter ] ) > 0 ){

            String swap = names[ counter - 1];
            names[ counter - 1 ] =  names[ counter ];
            names[ counter ] = swap;
            swapped = true;
        }
        bubbleSort( ++counter , names, swapped );
    }
}
